package com.hebeu.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.hebeu.device.dao.DMaintenanceMapper;
import com.hebeu.device.dao.DServiceMapper;
import com.hebeu.device.pojo.DMaintenance;
import com.hebeu.device.pojo.DMaintenanceExample;
import com.hebeu.device.pojo.DService;
import com.hebeu.device.service.DMaintenanceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class DMaintenanceServiceImpl implements DMaintenanceService {

    @Resource
    private DMaintenanceMapper dMaintenanceMapper;
    @Resource
    private DServiceMapper dServiceMapper;

    @Override
    public List<DMaintenance> selectDMaintenanceListByMainPerson(DMaintenance dMaintenance) {
        PageHelper.startPage(dMaintenance.getPageNo(), dMaintenance.getPageSize());
        DMaintenanceExample dMaintenanceExample = new DMaintenanceExample();
        DMaintenanceExample.Criteria criteria = dMaintenanceExample.createCriteria();
        //写入维修人员查询条件
        criteria.andMainPersonEqualTo(dMaintenance.getMainPerson());
        if(dMaintenance.getMainId()!=null)
            criteria.andMainIdEqualTo(dMaintenance.getMainId());
        if (dMaintenance.getMainStatus()!=null&&dMaintenance.getMainStatus()!=0){
            criteria.andMainStatusEqualTo(dMaintenance.getMainStatus());
        }
        //执行查询语句
        List<DMaintenance> dMaintenanceList = dMaintenanceMapper.selectByExample(dMaintenanceExample);
        return dMaintenanceList;
    }

    @Override
    public DMaintenance selectDMaintenanceByMainId(Integer mainId) {

        return dMaintenanceMapper.selectByPrimaryKey(mainId);
    }

    @Override
    public void updateDMaintenance(DMaintenance dMaintenance) {

        //若已完成维修，修改保修记录状态
        if (dMaintenance.getMainStatus()!=null&&dMaintenance.getMainStatus()==2){
            DService dService = new DService();
            dService.setsId(dMaintenance.getServiceId());
            //修改保修记录已完成
            dService.setsStatus(3);
            try {
                dServiceMapper.updateByPrimaryKeySelective(dService);
            }catch (Exception e){
                e.printStackTrace();
                throw new RuntimeException("修改保修记录失败！");
            }
        }
        try {
            dMaintenance.setAlterTime(new Date());
            dMaintenanceMapper.updateByPrimaryKeySelective(dMaintenance);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("修改维修记录失败！");
        }
    }

    @Override
    public List<DMaintenance> selectDMaintenanceList(DMaintenance dMaintenance) {
        PageHelper.startPage(dMaintenance.getPageNo(), dMaintenance.getPageSize());
        DMaintenanceExample dMaintenanceExample = new DMaintenanceExample();
        //执行查询语句
        return dMaintenanceMapper.selectByExample(dMaintenanceExample);
    }
}
